package com.gmall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gmall.common.result.Result;
import com.gmall.model.product.SkuInfo;
import com.gmall.product.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/product/sku")
public class SkuController {


    @Autowired
    SkuService skuService;
    @PostMapping("saveSkuInfo")
    public Result<String> saveSkuInfo(@RequestBody SkuInfo skuInfo){

        return skuService.saveSkuInfo(skuInfo);
    }

    @GetMapping("{pageNo}/{pageSize}")
    public Result<IPage<SkuInfo>> getSkuInfoPageByCategory3Id(@PathVariable Integer pageNo,
                                                              @PathVariable Integer pageSize,
                                                              @RequestParam Map searchMap){
        return skuService.getSkuInfoPage(pageNo,pageSize,searchMap);
    }

    @GetMapping("getSkuInfoById/{skuId}")
    public Result<SkuInfo> getSkuInfoById(@PathVariable Long skuId){
        return skuService.getSkuInfoById(skuId);
    }

    @PostMapping("onSale/{skuId}")
    public Result<String> onSale(@PathVariable Long skuId){

        return skuService.onSale(skuId);
    }

    @PostMapping("cancelSale/{skuId}")
    public Result<String> cancelSale(@PathVariable Long skuId){

        return skuService.cancelSale(skuId);
    }


    @PostMapping("getSkuInfoListAfterDateTime")
    public List<SkuInfo> getSkuInfoListAfterDateTime(@RequestBody Map searchMap) {
        Date lastSynDateTime = null;

        if(searchMap!=null){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String lastSynDateTime_String = searchMap.get("lastSyncDateTime").toString();

            if(!StringUtils.isEmpty(lastSynDateTime_String)){
                try {
                    lastSynDateTime = simpleDateFormat.parse(lastSynDateTime_String);
                } catch (ParseException e) {
                }
            }
        }
        return skuService.getSkuInfoListAfterDateTime(lastSynDateTime);//增量数据
    }
}
